﻿using UnityEngine;
using System.Collections;

// 04-23-2014 Update from Ramses Brown
// The Weapon fires by spawning a prefab at a specific position. The prefab has its own script which will make it move. The firing function only relates to the actual spawning of it.
// The Ship Now has an empty Game Object called Weapon Firing Position. Its function is to serve as the positioning for where the bullet spawns.


public class CharacterMovement : MonoBehaviour 
{
    // shipSpeed is for movement Speed, Rotation is for Rotation Speed, and shipPosition refers to the actual location of the ship on the screen.
    public int shipSpeed;
    private int rotationSpeed;
    public float shipPosition; 
	public AudioClip shotSound;

	public int maxRotationSpeed ;
	public int rotationModifier ;

    // I don't know any specifics on how the shot firing will be so I'm doing a basic single shot script.

    public GameObject weaponShot;
    public Transform weaponShotPosition;

    // Fire Rate will determine how fast the weapon fires. NextFire refers to how much of a delay there will be between shots
    public float fireRate;
    private float nextFire;
 
	public static Transform playerShip;
	public int shipOffset;

	public Texture healthBar;

	// Use this for initialization
	void Start () 
    {
		playerShip = transform;
		rotationSpeed = 75;
		maxRotationSpeed = 400;
		rotationModifier = 20;

	}
	
	// Update is called once per frame
	void Update () 
    {
		////////////////////////////////////////////////////////////////////////////////////
		///Player Shooting
		////////////////////////////////////////////////////////////////////////////////////

		if (Input.GetKey(KeyCode.Space) && Time.time > nextFire)
		{
			nextFire = Time.time + fireRate;	
			audio.PlayOneShot(shotSound);
			Instantiate(weaponShot, weaponShotPosition.position, weaponShotPosition.rotation);			
		} 

        ////////////////////////////////////////////////////////////////////////////////////
		///Player Movement
		////////////////////////////////////////////////////////////////////////////////////

        if (Input.GetKey("w") || Input.GetKey("up"))
        {
			rigidbody.AddRelativeForce(Vector3.back * shipSpeed * Time.deltaTime);            
        }
        if (Input.GetKey("s") || (Input.GetKey("down")))
        {
			rigidbody.AddRelativeForce(Vector3.forward * shipSpeed * Time.deltaTime);
        }        
        if (Input.GetKey("d") || (Input.GetKey("right")))
        {
			if(rotationSpeed < maxRotationSpeed)
			{
				transform.Rotate(Vector3.up * rotationSpeed, rotationSpeed * Time.deltaTime);
				rotationSpeed+=rotationModifier;
			}
			else
			{
				rotationSpeed = maxRotationSpeed;
				transform.Rotate(Vector3.up * rotationSpeed, rotationSpeed * Time.deltaTime);
			}
        }
        if (Input.GetKey("a") || (Input.GetKey("left")))
        {
			if(rotationSpeed < maxRotationSpeed)
			{
				transform.Rotate(Vector3.down * rotationSpeed, rotationSpeed * Time.deltaTime);
				rotationSpeed+=rotationModifier;
			}
			else
			{
				rotationSpeed = maxRotationSpeed;
				transform.Rotate(Vector3.down * rotationSpeed, rotationSpeed * Time.deltaTime);
			}
		}
		if (Input.GetKeyUp("a") || Input.GetKeyUp ("left") || Input.GetKeyUp("d") || Input.GetKeyUp("right"))
		{
			rotationSpeed = 200;
		}

		////////////////////////////////////////////////////////////////////////////////////
		///Wrap player ship across the screen when leaving field of view
		////////////////////////////////////////////////////////////////////////////////////

		if(playerShip.position.x >= 38)
		{
			playerShip.position = new Vector3(-38, playerShip.position.y, playerShip.position.z);
		}
		else if(playerShip.position.x <= -38)
		{
			playerShip.position = new Vector3(38, playerShip.position.y, playerShip.position.z);
		}
		else if(playerShip.position.y >= 20)
		{
			playerShip.position = new Vector3(playerShip.position.x,-20, playerShip.position.z);
		}
		else if(playerShip.position.y <= -20)
		{
			playerShip.position = new Vector3(playerShip.position.x,20, playerShip.position.z);
		}

	}
	void OnGUI()
	{
		//GUI.DrawTexture (new Rect (playerShip.position.x, playerShip.position.y, GameManager.playerHealth*1.5f, 80.0f), healthBar);
		Debug.Log (playerShip.position.x);
	}
}
